package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.Role;
import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.request.common.CommonUserRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公共控制器
 *
 * @author 柳曾雄
 * @date 2018-08-04
 */
@RestController
@RequestMapping(value = "/common")
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @Autowired
    private NodeService nodeService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "/download")
    public void download (@RequestParam("name") String name, HttpServletResponse response) {
        commonService.download(name, response);
    }

    @RequestMapping(value = "/upload")
    public JsonResponse upload (@RequestParam("file") MultipartFile file, User user) {
        String filename = commonService.upload(file, user);
        return JsonResponse.success(filename);
    }

    @RequestMapping(value = "/roles")
    public JsonResponse roles () {
        return JsonResponse.success(roleService.findAll());
    }

    @RequestMapping(value = "/users")
    public JsonResponse users (@RequestBody CommonUserRequest request, User user) {
        return JsonResponse.success(userService.findUser(user));
    }

    @RequestMapping(value = "/groups")
    public JsonResponse groups (User user) {
        return JsonResponse.success(groupService.findGroup(user));
    }

    @RequestMapping(value = "/nodes")
    public JsonResponse nodes () {
        return JsonResponse.success(nodeService.findAllSort());
    }

    @RequestMapping(value = "/organizations")
    public JsonResponse organizations () {
        return JsonResponse.success(organizationService.findAll());
    }
}
